package com.lzy.tricbuild3_ac;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 检验AC
 */
public class TestAcAndRegex {

	// 循环创建1000个敏感字符串
	// 匹配一百次。

	/**
	 * 方法1：使用正则
	 * @param compareStr
	 * @param text
	 */
	public List<String> useRegex(List<String> compareStr, String text) {
		List<String> ans = new ArrayList<>();

		for (String s : compareStr) {
			Pattern pattern = Pattern.compile(s);
			Matcher matcher = pattern.matcher(text);
			if(matcher.find()) {
				ans.add(s);
			}
		}

		return ans;
	}

	// 方法2：使用AC
	public List<String> useAC(AcTric acTric, String text) {
		return acTric.match(text.toCharArray());
	}

	// 方法3：使用indexOf
	public List<String> useIndexOf(List<String> compareStr, String text) {
		List<String> ans = new ArrayList<>();
		for (String s : compareStr) {
			if(text.contains(s)) {
				ans.add(s);
			}
		}
		return ans;
	}

	// 方法4: 直接替换
	public String useReplace(List<String> compareStr, String text) {
		for (String s : compareStr) {
			text = text.replaceAll(s, "**");
		}
		return text;
	}


	public static void main(String[] args) {

		TestAcAndRegex test = new TestAcAndRegex();

		List<String> strList = new ArrayList<>();
		for (int i = 0; i < 50000; i++) {
			strList.add(test.initText(100));
		}

//		String text = test.initText(200)+strList.get(4)+test.initText(200) + strList.get(3);
		String text = "";
		Random random = new Random();
		for (int i = 0; i < 100; i++) {
			if(i%15 == 0) {
				text+=strList.get(random.nextInt(50000));
			}
			text+= test.initText(random.nextInt(100));
		}


		// 初始化字典树
		AcTric acTric = new AcTric();
		for (String s : strList) {
			acTric.insert(s.toCharArray());
		}
		acTric.buildFailurePointer();

		long start;
		long end;
		start = System.currentTimeMillis();
		List<String> listIndexOf = test.useIndexOf(strList, text);
		end = System.currentTimeMillis();

		System.out.println("indexOf ---> " + (end-start));

		start = System.currentTimeMillis();
		List<String> listRegex = test.useRegex(strList, text);
		end = System.currentTimeMillis();

		System.out.println("regex ---> " + (end-start));

		start = System.currentTimeMillis();
		List<String> listAC = acTric.match(text.toCharArray());
		String acReplace = test.useReplace(listAC, text);
		end = System.currentTimeMillis();

		System.out.println("AC ---> " + (end-start));

		start = System.currentTimeMillis();
		String  replaceStr = test.useReplace(strList,text);
		end = System.currentTimeMillis();

		System.out.println("Replace ---> " + (end-start));

		System.out.println(listIndexOf);
		System.out.println(listRegex);
		System.out.println(listAC);

//		indexOf ---> 97
//		regex ---> 1009
//		AC ---> 2
//		Replace ---> 130
//		[hmxlbvqvpqgmmojtsqzmoujrmaadluaevisyyrxgrtbuzspbfuizomrcbhecgombhrwiutvbcdeeqrvilnbrvhveffwhktnbuazu, wwinqlbcwbrvcfllbxqxcrsscrtmqkamibsnsqaflvpmcjdbisycgqhyzsvjnqbhxwlhbeaigpvhywncggbmflodmbggaawhnjxt, tscenwdgjfvcwpissvbcjqifkhnhtdgsrjyzwxkirvythboxqpqhhynvnmkmlomwaahsjvmndxkzzhkrhizmgtjmhnvwbjkkydhz, mwpyguzucvwajcpexbcczochbrecvcqcwpuopvlvdammdaaricvdfvjgsazutlgrpabmluhwrptmueufoibzjudvnyyamccmdzsn, atpzkczksdohvesyiiebsqhtqssthvhnsolhigaxbxwuibrvgnyaulutariipnvpbwkinddcguvdnuzveczlupwucehvusmdccwl, wesberptwxthlyhylktftcrsykaapxbstimrplzcvusnrgafzsxbykthahndoopcwhipdzzqamkdhgaziblgtqfhdcdpvewemovf, gumixpwngwpfhzaooasxsnivnebcejntgugeprvqnnnmcqcqkzllxioublaagwykssewnkueqzefaatggzrkslqhdyiavoxcjgsy]
//		[hmxlbvqvpqgmmojtsqzmoujrmaadluaevisyyrxgrtbuzspbfuizomrcbhecgombhrwiutvbcdeeqrvilnbrvhveffwhktnbuazu, wwinqlbcwbrvcfllbxqxcrsscrtmqkamibsnsqaflvpmcjdbisycgqhyzsvjnqbhxwlhbeaigpvhywncggbmflodmbggaawhnjxt, tscenwdgjfvcwpissvbcjqifkhnhtdgsrjyzwxkirvythboxqpqhhynvnmkmlomwaahsjvmndxkzzhkrhizmgtjmhnvwbjkkydhz, mwpyguzucvwajcpexbcczochbrecvcqcwpuopvlvdammdaaricvdfvjgsazutlgrpabmluhwrptmueufoibzjudvnyyamccmdzsn, atpzkczksdohvesyiiebsqhtqssthvhnsolhigaxbxwuibrvgnyaulutariipnvpbwkinddcguvdnuzveczlupwucehvusmdccwl, wesberptwxthlyhylktftcrsykaapxbstimrplzcvusnrgafzsxbykthahndoopcwhipdzzqamkdhgaziblgtqfhdcdpvewemovf, gumixpwngwpfhzaooasxsnivnebcejntgugeprvqnnnmcqcqkzllxioublaagwykssewnkueqzefaatggzrkslqhdyiavoxcjgsy]
//		[hmxlbvqvpqgmmojtsqzmoujrmaadluaevisyyrxgrtbuzspbfuizomrcbhecgombhrwiutvbcdeeqrvilnbrvhveffwhktnbuazu, mwpyguzucvwajcpexbcczochbrecvcqcwpuopvlvdammdaaricvdfvjgsazutlgrpabmluhwrptmueufoibzjudvnyyamccmdzsn, atpzkczksdohvesyiiebsqhtqssthvhnsolhigaxbxwuibrvgnyaulutariipnvpbwkinddcguvdnuzveczlupwucehvusmdccwl, wesberptwxthlyhylktftcrsykaapxbstimrplzcvusnrgafzsxbykthahndoopcwhipdzzqamkdhgaziblgtqfhdcdpvewemovf, gumixpwngwpfhzaooasxsnivnebcejntgugeprvqnnnmcqcqkzllxioublaagwykssewnkueqzefaatggzrkslqhdyiavoxcjgsy, wwinqlbcwbrvcfllbxqxcrsscrtmqkamibsnsqaflvpmcjdbisycgqhyzsvjnqbhxwlhbeaigpvhywncggbmflodmbggaawhnjxt, tscenwdgjfvcwpissvbcjqifkhnhtdgsrjyzwxkirvythboxqpqhhynvnmkmlomwaahsjvmndxkzzhkrhizmgtjmhnvwbjkkydhz]

	// azu,jxt,dhz,zsn,cwl,ovf,gsy
	// azu,jxt,dhz,zsn,cwl,ovf,gsy

	}


	// 生成随机的字符串
	public String initText(int n) {
		Random random = new Random();
		String s = "";
		for(int i = 0; i < n; i++) {
			int len = random.nextInt(26);
			s = s + ((char)('a'+len));
		}
		return s;
	}

}
